<?php

/**
 * This is the model class for table "orders".
 *
 * The followings are the available columns in table 'orders':
 * @property string $id
 * @property string $user_id
 * @property string $email
 * @property string $firstname
 * @property string $lastname
 * @property string $shipping_address
 * @property string $ip
 * @property string $forwarded_ip
 * @property string $user_agent
 * @property string $note
 * @property double $total
 * @property integer $status
 * @property integer $create_time
 * @property integer $deleted
 *
 * The followings are the available model relations:
 * @property Products[] $products
 * @property User $user
 */
class Order extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Order the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'orders';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('email, firstname, lastname, shipping_address, total, status', 'required'),
			array('status, create_time, deleted', 'numerical', 'integerOnly'=>true),
			array('total', 'numerical'),
			array('user_id', 'length', 'max'=>11),
			array('email, firstname, lastname', 'length', 'max'=>50),
			array('shipping_address, user_agent', 'length', 'max'=>255),
			array('ip, forwarded_ip', 'length', 'max'=>25),
			array('note', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, user_id, email, firstname, lastname, shipping_address, ip, forwarded_ip, user_agent, note, total, status, create_time, deleted', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'products' => array(self::MANY_MANY, 'Products', 'order_details(order_id, product_id)'),
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => Yii::t('labels','ID'),
			'user_id' => Yii::t('labels','Khách hàng'),
			'email' => Yii::t('labels','Email'),
			'firstname' => Yii::t('labels','Họ'),
			'lastname' => Yii::t('labels','Tên'),
			'shipping_address' => Yii::t('labels','Địa chỉ giao hàng'),
			'ip' => Yii::t('labels','IP'),
			'forwarded_ip' => Yii::t('labels','Forwarded IP'),
			'user_agent' => Yii::t('labels','User Agent'),
			'note' => Yii::t('labels','Ghi chú'),
			'total' => Yii::t('labels','Thành tiền'),
			'status' => Yii::t('labels','Trạng thái'),
			'create_time' => Yii::t('labels','Ngày mua'),
			'deleted' => Yii::t('labels','Xóa'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('firstname',$this->firstname,true);
		$criteria->compare('lastname',$this->lastname,true);
		$criteria->compare('shipping_address',$this->shipping_address,true);
		$criteria->compare('ip',$this->ip,true);
		$criteria->compare('forwarded_ip',$this->forwarded_ip,true);
		$criteria->compare('user_agent',$this->user_agent,true);
		$criteria->compare('note',$this->note,true);
		$criteria->compare('total',$this->total);
		$criteria->compare('status',$this->status);
		$criteria->compare('create_time',$this->create_time);
		$criteria->compare('deleted',$this->deleted);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}